//
//  SXTLandingInPutView.m
//  8.18项目
//
//  Created by ma c on 16/8/18.
//  Copyright © 2016年 zhang. All rights reserved.
//

#import "SXTLandingInPutView.h"

@interface SXTLandingInPutView ()<UITextFieldDelegate>

//手机号
@property (nonatomic ,strong) UILabel * tostlabel;
@property (nonatomic ,strong) UITextField * userPhoneText;
@property (nonatomic ,strong) UITextField * passwordText;
@property (nonatomic ,strong) UIButton * nextViewBtn;
@property (nonatomic ,strong) UIButton * goLoginbtn;
@property (nonatomic ,strong) UILabel * whiteBacklabe;
@property (nonatomic ,strong) UILabel * grayLineLabel;

@end



@implementation SXTLandingInPutView
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.whiteBacklabe];
        [self addSubview:self.tostlabel];
        [self addSubview:self.userPhoneText];
        [self addSubview:self.passwordText];
        [self addSubview:self.nextViewBtn];
        [self addSubview:self.goLoginbtn];
        [self addSubview: self.grayLineLabel];

    }
    return self;
}

//布局
-(void)layoutSubviews{
    
    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    [_tostlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.height.mas_equalTo(35);
        make.right.equalTo(weakSelf.mas_right);
    }];
    [_whiteBacklabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tostlabel.mas_bottom);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
        make.height.equalTo(@89);
    }];
    [_userPhoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.whiteBacklabe.mas_top);
        make.size.mas_equalTo(CGSizeMake(VIEW_WIDTH, 44));
    }];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.userPhoneText.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(VIEW_WIDTH, 44));
    }];
    
    [_grayLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.userPhoneText.mas_bottom);
    }];
    [_nextViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(15);
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.height.equalTo(@35);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
    }];
    [_goLoginbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nextViewBtn.mas_bottom).offset(23);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.size.mas_equalTo(CGSizeMake(70, 30));
    }];
    
}


#pragma mark - 初始化
-(UILabel *)tostlabel {
    if (!_tostlabel) {
        _tostlabel = [[UILabel alloc]init];
        _tostlabel.text = @"请输入手机号码注册新用户";
        _tostlabel.textColor = [UIColor grayColor];
    }
    return _tostlabel;
}

- (UITextField *)userPhoneText{
    if (!_userPhoneText) {
        _userPhoneText = [[UITextField alloc]init];
        _userPhoneText.delegate = self;
        _userPhoneText.placeholder = @"请输入手机号码";
        _userPhoneText.keyboardType = UIKeyboardTypeNumberPad;
        [_userPhoneText addTarget:self action:@selector(userTextField:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _userPhoneText;
}

- (UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.placeholder = @"请输入密码";
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _passwordText;
}

-(UILabel *)whiteBacklabe{
    if (!_whiteBacklabe) {
        _whiteBacklabe = [[UILabel alloc]init];
        _whiteBacklabe.backgroundColor = [UIColor whiteColor];
        _whiteBacklabe.layer.borderColor = RGB(188, 186, 192).CGColor;
        _whiteBacklabe.layer.borderWidth = 1.0;
    }
    return _whiteBacklabe;
}

-(UILabel *)grayLineLabel{

    if (!_grayLineLabel) {
        _grayLineLabel = [[UILabel alloc]init];
        _grayLineLabel.backgroundColor = RGB(188, 186, 192);
    }
    return _grayLineLabel;
}

-(UIButton *)nextViewBtn{
    if (!_nextViewBtn) {
        _nextViewBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _nextViewBtn.backgroundColor = RGB(229, 229, 229);
        _nextViewBtn.layer.masksToBounds = YES;
        _nextViewBtn.layer.cornerRadius = 5.0;
        _nextViewBtn.userInteractionEnabled = NO;
        [_nextViewBtn setTitle:@"下一步" forState:(UIControlStateNormal)];
        [_nextViewBtn addTarget:self action:@selector(goNextViewController) forControlEvents:UIControlEventTouchUpInside];
        _nextViewBtn.titleLabel.textColor = RGB(132, 132, 132);
    }
    return _nextViewBtn;
}

-(UIButton *)goLoginbtn{

    if (!_goLoginbtn) {
        _goLoginbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _goLoginbtn.backgroundColor = [UIColor clearColor];
        [_goLoginbtn setTitle:@"去登录" forState:(UIControlStateNormal)];
        [_goLoginbtn setTitleColor:RGB(56, 165, 241) forState:(UIControlStateNormal)];
    }
    return _goLoginbtn;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _userPhoneText && range.location == 11) {
        return NO;
    }
    return YES;

}

- (void)goNextViewController{
    NSLog(@"我已经活了，可以进行点击了");
    if (_nextBlock) {
        _nextBlock(@{@"userName":_userPhoneText.text,@"password":_passwordText.text});
    }
}


-(void)userTextField:(UITextField *)textField {
    if (textField.text.length == 11 && _passwordText.text.length) {
        _nextViewBtn.backgroundColor = RGB(56, 166, 241);
        _nextViewBtn.userInteractionEnabled = YES;
    }else{
    
        _nextViewBtn.backgroundColor = RGB(229,229, 229);
        _nextViewBtn.userInteractionEnabled = NO;
    }

}

- (void)passwordTextChangeText:(UITextField *)textField{

    if (textField.text.length > 5 && _userPhoneText.text.length ==11) {
        _nextViewBtn.backgroundColor = RGB(56, 166, 241);
        _nextViewBtn.userInteractionEnabled = YES;
    }else{
    
        _nextViewBtn.backgroundColor = RGB(229, 229, 229);
        _nextViewBtn.userInteractionEnabled = NO;
    }

}




@end
